package com.orm.analysis.action;

import com.opensymphony.xwork2.ActionContext;
import com.orm.analysis.model.*;
import com.orm.analysis.service.GenericService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class AddressAction extends FrontendAction {

    private GenericService<Address> service;
    private GenericService<InternationalAddress> interAddressservice;
    private Address address;
    private InternationalAddress interAddress;
    private AddressType[] addressTypeList;
    private List<Address> addresses;
    private List<InternationalAddress> interAddresses;


    public AddressAction(GenericService<Address> service, GenericService<InternationalAddress> interAddressservice) {
        this.service = service;
        this.interAddressservice = interAddressservice;
    }

    public String getAllAddresses() {
        addresses = service.list(page, size);
        return SUCCESS;
    }

    public String getAllInternationalAddresses() {
        interAddresses = interAddressservice.list(page, size);
        return SUCCESS;
    }

    public String list() {
        addresses = service.list(page, size);
        return SUCCESS;

    }

    public String setUpForInsert() {
        prep();
        return "success";
    }

    public String setUpForInsertInterAddres() {
        prep();
        return "success";
    }

    public String setUpForUpdate() {
        prep();
        if (address != null && address.getAddressId() != null) {

            address = service.getById(address.getAddressId());
        }
        return "success";
    }

    public String setUpForUpdateInterAddres() {
        prep();
        if (interAddress != null && interAddress.getAddressId() != null) {

            interAddress = interAddressservice.getById(interAddress.getAddressId());
        }
        return "success";
    }

    public String insert() {

        if (!validationSuccessful(address)) {
            return "input";
        } else {
            if (address.getAddressId() == null) {
                service.create(address);
            }



        }
        return "success";
    }

    public String interAddressInsert() {

        if (!validationSuccessful(interAddress)) {
            return "input";
        } else {
            if (interAddress.getAddressId() == null) {
                interAddressservice.create(interAddress);
            }



        }
        return "success";
    }

    public String update() {

        if (!validationSuccessful(address)) {
            return "input";
        } else {
            service.update(address);

        }
        return "success";
    }

    public String interAddressUpdate() {

        if (!validationSuccessful(interAddress)) {
            return "input";
        } else {
            interAddressservice.update(interAddress);

        }
        return "success";
    }



    public String getSingleAddress() {
//        address = service.findWithDepth("addressId",address.getAddressId());
        address = service.findWithDepth("addressId",new Integer(1) );
//        address = service.findWithDepth("addressId",new Integer(10) );
        addresses = new ArrayList<Address>();
               addresses.add(address);

       return SUCCESS;
   }
   public String getSingleAddressTwice() {


//       address = service.findWithDepth("addressId",address.getAddressId());
       address = service.findWithDepth("addressId",new Integer(1)  );
       address = service.findWithDepth("addressId",new Integer(1)  );
       addresses = new ArrayList<Address>();
       addresses.add(address);

       return SUCCESS;
   }

    public String getCRUD() {


            if (!validationSuccessful(address)) {
                        return "input";
                    } else {
                        if (address.getAddressId() == null) {
                            address = service.create(address);
                            address =service.getById(address);
                            address.setStreet("Jayasuriya Mw");
                            address =service.update(address);
                            service.delete(address.getAddressId());
                        }

                    }



            return SUCCESS;
        }

    private void prep() {
        addressTypeList = new AddressType[4];
        addressTypeList[0] = AddressType.BILLING;
        addressTypeList[1] = AddressType.HOME;
        addressTypeList[2] = AddressType.OFFICE;
        addressTypeList[3] = AddressType.SHIPPING;

        Map session = ActionContext.getContext().getSession();
        session.put("addressTypeList", addressTypeList);
    }

    public String delete() {
        service.delete(address.getAddressId());
        return "success";
    }

    public String interAddressDelete() {
        interAddressservice.delete(address.getAddressId());
        return "success";
    }

    public String addressBulkTask() {
        try {
            service.executeImproter();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    public String getBulkAddress() {
        try {
            addresses = service.findAllWithDepth();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    public String create() {
        service.create(address);
        return SUCCESS;
    }

    private boolean validationSuccessful(Address address) {

        if (address.getStreet() == null || address.getStreet().trim().length() < 1) {
            addActionMessage("Street is required");
        }
        if (address.getZipcode() == null || address.getZipcode().trim().length() < 1) {
            addActionMessage("Zipcode is required");
        }

        if (this.hasActionMessages()) {
            return false;
        } else {
            return true;
        }

    }

    private boolean validationSuccessful(InternationalAddress address) {

        if (address.getStreet() == null || address.getStreet().trim().length() < 1) {
            addActionMessage("Street is required");
        }
        if (address.getZipcode() == null || address.getZipcode().trim().length() < 1) {
            addActionMessage("Zipcode is required");
        }

        if (this.hasActionMessages()) {
            return false;
        } else {
            return true;
        }

    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public InternationalAddress getInterAddress() {
        return interAddress;
    }

    public void setInterAddress(InternationalAddress interAddress) {
        this.interAddress = interAddress;
    }

    public List<InternationalAddress> getInterAddresses() {
        return interAddresses;
    }

    public void setInterAddresses(List<InternationalAddress> interAddresses) {
        this.interAddresses = interAddresses;
    }
}
